package com.taikang.service.impl;

import com.taikang.dao.TaskRecordDao;
import com.taikang.dao.TrainCourseDao;
import com.taikang.dao.EmployeeDao;
import com.taikang.dao.UserDao;
import com.taikang.entity.CourseRecord;
import com.taikang.entity.Employee;
import com.taikang.entity.TaskRecord;
import com.taikang.entity.User;
import com.taikang.service.EmployeeService;
import com.taikang.util.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhai on 16/5/10.
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private EmployeeDao employeeDao;
    private TrainCourseDao trainCourseDao;
    private TaskRecordDao taskRecordDao;
    private UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Autowired
    public void setTaskRecordDao(TaskRecordDao taskRecordDao) {
        this.taskRecordDao = taskRecordDao;
    }

    @Autowired
    public void setTrainCourseDao(TrainCourseDao trainCourseDao) {
        this.trainCourseDao = trainCourseDao;
    }

    @Autowired
    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    @Override
    public Employee getEmployeeById(Integer employeeId) {
        return employeeDao.fetch(employeeId);
    }

    @Override
    public Employee employeeLogin(String username, String password) {
        return employeeDao.employeeLogin(username, password);
    }

    @Override
    public Object getSalaryByEmployeeId(Integer employeeId, Integer recordId, Integer page, Integer size) {
        return employeeDao.getSalaryByEmployeeId(employeeId, recordId, page, size);
    }

    @Override
    public Object getTrainInfo(Integer agentId, Integer courseId, Integer page, Integer size) {
        return employeeDao.getTrainInfo(agentId, courseId, page, size);
    }

    @Override
    public Object getTaskInfo(Integer employeeId, Integer taskId, Integer page, Integer size) {
        Object o = employeeDao.getTaskInfo(employeeId, taskId, page, size);
        if(o instanceof Pagination) {
            List<TaskRecord> list = (List<TaskRecord>) ((Pagination) o).getDataList();
            for(TaskRecord taskRecord : list) {
                User user = userDao.fetch(taskRecord.getUserID());
                taskRecord.setUserName(user.getUserName());
            }
            ((Pagination) o).setDataList(list);
        } else {
            TaskRecord taskRecord = (TaskRecord) o;
            taskRecord.setUserName(userDao.fetch(taskRecord.getUserID()).getUserName());
        }
        return o;
    }

    @Override
    public Boolean enrollTrain(Integer employeeId, Integer courseId) {
        CourseRecord courseRecord = new CourseRecord();
        courseRecord.setCourseID(courseId);
        courseRecord.setEmployeeID(employeeId);
        courseRecord.setCourse(trainCourseDao.fetch(courseId).getCourseName());
        courseRecord.setEmployee(employeeDao.fetch(employeeId).getEmployeeName());
        return trainCourseDao.enroll(courseRecord);
    }

    @Override
    public void updateTask(TaskRecord record) {
        taskRecordDao.update(record);
    }
}
